/*
 *  $TEAM_NAME Space Trader
 */
package views;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;



import models.Approach;
import models.Encounter;
import models.Encounter.Pirate;
import models.Game;

/**
 * Ecounter Panel
 * @author twb
 *@version 1.00
 */
public class EncounterPanel extends JPanel {
	/**
	 * field
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * int 100
	 */
	private static final int ONE_HONDO=100;
	
	/**
	 * int 75
	 */
	private static final int SEVENTY_FIVE=75;
	
	/**
	 * int 250
	 */
	private static final int TWO_FIDDY=250;
	
	/**
	 * int 250
	 */
	private static final int TWO_EIGHTY=280;
	
	/**
	 * int 300
	 */
	private static final int THREE_HONDO=300;
	
	/**
	 * int 20
	 */
	private static final int TWENTY=20;
	
	/**
	 * int 50
	 */
	private static final int FIDDY=50;
	
	/**
	 * int 265
	 */
	private static final int TWO_SIX_FIVE=265;
	
	/**
	 * int 530
	 */
	private static final int FIVE_THIRTY=530;
	
	/**
	 * int 115
	 */
	private static final int ONE_FIFTEEN=115;
	
	/**
	 * int 130
	 */
	private static final int ONE_THIRTY=130;
	
	/**
	 * int 160
	 */
	private static final int ONE_SIXTY=160;
	
	/**
	 * int 25
	 */
	private static final int TWENTY_FIVE=25;
	
	/**
	 * int 40
	 */
	private static final int FOURTY=40;
	
	/**
	 * field
	 */
	private final Approach approach;
	
	/**
	 * field
	 */
	private Encounter encounter;
	
	/**
	 * field
	 */
	private JLabel playerShip, characterShip;
	
	/**
	 * field
	 */
	private final JLabel lblDistance, lblZero, lblDistanceString, 
	    lblDesc, lblAction;

	/**
	 * field
	 */
	private final JButton btnSurrender, btnFlee, btnAttack, btnNextTurn;
	
	/**
	 * field
	 */
	private final JProgressBar barProgress, barPlayerHealth; 
	
	/**
	 * field
	 */
	private JProgressBar barCharacterHealth;
	
	/**
	 * field
	 */
	private BufferedImage backgroundImage = null;
	
	/**
	 * Creates a new encounter panel for the encounter
	 * @param approach Approach
	 * @param encounter Encounter
	 */
	public EncounterPanel(Approach approach, Encounter encounter) {
		this.approach = approach;
		this.encounter = encounter;
		
		super.setLayout(null);
		
		try {
            backgroundImage = ImageIO.read(new File("resources/MapBackground.png"));
        } catch (IOException e) {
            System.err.println(e);		//Should we throw an exception here instead?
            System.exit(1);
        }
		
		//Create the labels
		this.lblAction = new JLabel();
		this.lblDesc = new JLabel(
				"You are on your way to " + this.approach.getSs().getName());
		this.lblDistance = new JLabel(
				String.valueOf(approach.getProgress().getMaximum()));
		this.lblDistanceString = new JLabel("Distance to Space Port: " 
				+ (this.approach.getProgress().getMaximum() 
						- this.approach.getProgress().getValue()) 
				+ " turns.");
		this.lblZero = new JLabel("0");
		
		//fomate the labels
		this.lblAction.setForeground(Color.LIGHT_GRAY);
		this.lblDesc.setForeground(Color.LIGHT_GRAY);
		this.lblDistance.setForeground(Color.LIGHT_GRAY);
		this.lblDistanceString.setForeground(Color.LIGHT_GRAY);
		this.lblZero.setForeground(Color.LIGHT_GRAY);
		
		//Create the Images
		try {
		final Game currentGame=this.approach.getGame();
			final File imageFile = new File("resources/" 
		+ currentGame.getPlayer().getShip().getName() 
		+ ".jpg");
			final Image shipImage = ImageIO.read(imageFile).getScaledInstance(
					ONE_HONDO, ONE_HONDO, Image.SCALE_DEFAULT);
			playerShip = new JLabel(new ImageIcon(shipImage));
	     } catch (Exception e) {
			System.err.println(e);
	    	 playerShip = new JLabel(); 
	     }
		
		//Create the JButtons
		this.btnAttack = new JButton("Attack");
		this.btnFlee = new JButton("Flee");
		this.btnSurrender = new JButton("Surrender");
		this.btnNextTurn = new JButton("Next Turn");
		
		//Create the JProgressBar
		this.barProgress = new JProgressBar(approach.getProgress());
		final Game currentGame=this.approach.getGame();
		this.barPlayerHealth = new JProgressBar(
				currentGame.getPlayer().getShip().getHealth());
		
		//Lay everything out
		this.barProgress.setBounds(SEVENTY_FIVE, TWO_FIDDY, THREE_HONDO, TWENTY);
		this.lblDistanceString.setBounds(SEVENTY_FIVE, TWO_EIGHTY, THREE_HONDO, TWENTY);
		this.lblDesc.setBounds(SEVENTY_FIVE, THREE_HONDO, TWO_FIDDY, FIDDY);
		this.btnNextTurn.setBounds(TWO_SIX_FIVE, FIVE_THIRTY, ONE_FIFTEEN, FOURTY);
		
		//Layout for pirate
		this.playerShip.setBounds(FIDDY, FIDDY, ONE_HONDO, ONE_HONDO);
		this.barPlayerHealth.setBounds(FIDDY, ONE_SIXTY, ONE_HONDO, TWENTY_FIVE);
		this.btnFlee.setBounds(TWO_SIX_FIVE, FIVE_THIRTY, ONE_FIFTEEN, FOURTY);
		this.btnAttack.setBounds(ONE_THIRTY, FIVE_THIRTY, ONE_FIFTEEN, FOURTY);
		
		//add everything
		this.update();
	}

	/**
	 * @return the encounter
	 */
	public final Encounter getEncounter() {
		return encounter;
	}

	/**
	 * @param encounter the encounter to set
	 */
	public void setEncounter(Encounter encounter) {
		
		this.encounter = encounter;
		if(encounter != null){
			if(this.encounter.getCharacter() instanceof Pirate){
				final Pirate pirate = (Pirate) this.encounter.getCharacter();
				
				try {
					final File imageFile = new File(
							"resources/" + pirate.getShip().getName() + ".jpg");
						final Image shipImage = ImageIO.read(imageFile).getScaledInstance(
								ONE_HONDO, ONE_HONDO, Image.SCALE_DEFAULT);
			        	characterShip = new JLabel(new ImageIcon(shipImage));
			     } catch (Exception e) {
						System.err.println(e);

			    	characterShip = new JLabel(); 
			     }
				
				characterShip.setBounds(THREE_HONDO, FIDDY, ONE_HONDO, ONE_HONDO);
				this.barCharacterHealth = new JProgressBar((
						(Pirate) this.encounter.getCharacter()).getShip().getHealth());
				this.barCharacterHealth.setBounds(
						THREE_HONDO, ONE_SIXTY, ONE_HONDO, TWENTY);
			}
		}
	}

	/**
	 * Adds Action listener
	 * @param listener
	 */
	public void addActionListener(ActionListener listener){
		this.btnAttack.addActionListener(listener);
		this.btnFlee.addActionListener(listener);
		this.btnSurrender.addActionListener(listener);
		this.btnNextTurn.addActionListener(listener);
	}
	
	/**
	 * 	Shows the gui for approach	
	 */
	public void showApproach(){
		this.lblDesc.setText("You are on your way to " + this.approach.getSs().getName());
		
		add(this.playerShip);
		add(this.barPlayerHealth);
		add(this.barProgress);
		add(this.lblDistanceString);
		add(this.lblDesc);
		add(this.btnNextTurn);
	}
	
	/**
	 * Creates a pirate image on the screen
	 */
	public void showPirate(){
		this.lblDesc.setText("You have encountered a Pirate!");
		
		add(this.playerShip);
		add(this.barPlayerHealth);
		add(this.characterShip);
		add(this.barCharacterHealth);
		add(this.barProgress);
		add(this.lblDistanceString);
		add(this.lblDesc);
		add(this.btnFlee);
		add(this.btnAttack);
	}
	
	/**
	 * Updates the view to show encounter progress
	 */
	public final void update(){
		this.lblDistanceString.setText("Distance to Space Port: " 
	+ (this.approach.getProgress().getMaximum() 
			- this.approach.getProgress().getValue()) 
			+ " turns.");
		
		this.removeAll();
		if(this.encounter != null){
			this.showPirate();
		} else {
			this.showApproach();
			
		}
		this.validate();
		this.repaint();
		
	}
	
	/**
	 * @param g
	 */
	public void paintComponent(Graphics g) {
        super.paintComponent(g);
        final Graphics2D gfx = (Graphics2D) g;

        gfx.setRenderingHint(
        		RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        final Dimension size = getSize();

        // Background
        gfx.drawImage(backgroundImage, 0, 0, size.width, size.height, Color.BLACK, null);
        
        this.barProgress.repaint();
        this.btnAttack.repaint();
        this.btnFlee.repaint();
        this.btnNextTurn.repaint();
        this.lblDesc.repaint();
        this.playerShip.repaint();
        if(this.characterShip != null){
        	this.characterShip.repaint();
        	this.barCharacterHealth.repaint();
        }
        this.barPlayerHealth.repaint();
        
    }

}